import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import cn.accp.entity.Student;

public class SetDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	/**
	 * 1. 键盘录入10名学生信息.
	 */
	public static Set<Student> demo1() {
		Scanner input = new Scanner(System.in);
		Random random = new Random();
		Set<Student> students = new HashSet<Student>();
		Student student;
		int id = 1001;
		for (int i = 0; i < 10; i++) {
			student = new Student();
			System.out.println("请输入学生学号:");
			// student.setId(input.nextInt());
			student.setId(id + i);
			System.out.println("请输入姓名:");
			// student.setName(input.next());
			student.setName("stu" + i);
			System.out.println("请输入Java科目成绩:");
			// student.setJavaScore(input.nextInt());
			student.setJavaScore(random.nextInt(80) + 20);
			System.out.println("请输入Html科目成绩:");
			// student.setHtmlScore(input.nextInt());
			student.setHtmlScore(random.nextInt(70) + 30);
			students.add(student);
		}

		return students;
	}

	/**
	 * 2. 使用3种遍历方式，输出学员信息
	 */
	public static void demo2() {
		Set<Student> students = demo1();
		Student student = null;
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			System.out.println(stu.toString());
		}
		System.out.println("第三种,使用Iterator迭代器循环遍历");
		Iterator<Student> iterator = students.iterator();
		while (iterator.hasNext()) {
			student = iterator.next();
			System.out.println(student.toString());
		}
	}

	/**
	 * 3.求出java考试最高分。求出html考试最低分.
	 */
	public static void demo3() {
		Set<Student> students = demo1();
		int maxJava = 0, minHtml = 100;
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			if (stu.getJavaScore() > maxJava) {
				maxJava = stu.getJavaScore();
			}
			if (stu.getHtmlScore() < minHtml) {
				minHtml = stu.getHtmlScore();
			}
			System.out.println(stu.toString());
		}
		System.out.println("Java科目最高分为:" + maxJava + ",html科目最低分为" + minHtml
				+ ".");
	}

	/**
	 * 4. 显示考试及格的同学，只需显示学号和姓名 。两门功课全部及格算及格。
	 */
	public static void demo4() {
		Set<Student> students = demo1();
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			System.out.println(stu.toString());
		}
		System.out.println("以下同学考试通过:");
		for (Student stu : students) {
			if (stu.getJavaScore() >= 60 && stu.getHtmlScore() > 60) {
				System.out.println("学号:" + stu.getId() + ",姓名:" + stu.getName()
						+ ";");
			}
		}
	}

	/**
	 * 5. 显示，总分最高的同学信息，注意，总分可能存在并列的情况，既多个第一名。
	 */
	public static void demo5() {
		Set<Student> students = demo1();
		int temp;
		int maxScore = 0;
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			temp = stu.getJavaScore() + stu.getHtmlScore();
			if (temp > maxScore) {
				maxScore = temp;
			}
			System.out.println(stu.toString());
		}
		System.out.println("以下同学成绩为第一名:");
		for (Student stu : students) {
			temp = stu.getJavaScore() + stu.getHtmlScore();
			if (temp == maxScore) {
				System.out.println("学号:" + stu.getId() + ",姓名:" + stu.getName()
						+ ",Java科目成绩:" + stu.getJavaScore() + ",html科目成绩:"
						+ stu.getHtmlScore() + ";");
			}
		}
	}

	/**
	 * 6. 输入学生姓名，查找学生成绩。
	 */
	public static void demo6() {
		Scanner input = new Scanner(System.in);
		Set<Student> students = demo1();
		boolean flag = true;
		String name;
		System.out.println("请输入要查询的学生姓名:");
		name = input.next();
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			if (stu.getName().equals(name)) {
				System.out.println("所查询的同学成绩如下:");
				System.out.println(stu.toString());
				flag = false;
			}
		}
		if (flag) {
			System.out.println("所要查询的学生姓名不存在!");
		}
	}

	/**
	 * 7. 输入学生学号，存在，就删除该学生。
	 */
	public static void demo7() {
		Scanner input = new Scanner(System.in);
		Set<Student> students = demo1();
		Student student = null;
		int id;
		System.out.println("请输入要查询的学生学号:");
		id = input.nextInt();
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			System.out.println(stu.toString());
		}
		System.out.println("第三种,使用Iterator迭代器循环遍历");
		Iterator<Student> iterator = students.iterator();
		while (iterator.hasNext()) {
			student = iterator.next();
			if (student.getId() == id) {
				students.remove(student);
				iterator = students.iterator();
			}
		}
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			System.out.println(stu.toString());
		}
	}

	/**
	 * 8. 输入学生学号，存在，修改对应的java成绩。
	 */
	public static void demo8() {
		Scanner input = new Scanner(System.in);
		Set<Student> students = demo1();
		Student student = null;
		boolean flag = false;
		System.out.println("第二种,增强for循环方式循环遍历");
		for (Student stu : students) {
			System.out.println(stu.toString());
		}
		System.out.println("请输入修改的学生学号:");
		int id = input.nextInt();
		System.out.println("第三种,使用Iterator迭代器循环遍历");
		Iterator<Student> iterator = students.iterator();
		while (iterator.hasNext()) {
			student = iterator.next();
			if (student.getId() == id) {
				flag = true;
				break;
			}
			System.out.println(student.toString());
		}
		if (flag) {
			System.out.println("请输入新的Java科目成绩:");
			int newJavaScore = input.nextInt();
			System.out.println("第二种,增强for循环方式循环遍历");
			for (Student stu : students) {
				if (stu.getId() == id) {
					stu.setJavaScore(newJavaScore);
				}
			}
		} else {
			System.out.println("所查找的学生不存在!");
		}
	}

	/**
	 * 9. 根据总分进行从高到底排序，并输出。
	 * 
	 * Set接口的实现类中的对象是无序,唯一的,不适应于排序
	 */
	public static void demo9() {
	}

}
